package edu.wpi.cs509.ui;

import edu.wpi.cs509.representation.Location;
import edu.wpi.cs509.representation.TeamColor;

/**
 * Transform location converts the sceen grid location to the game board Location
 * object.  The game board is always represented the same internally, with
 * Location 0,0 located on the bottom left corner of the Red player's side
 * of the game board.
 * 
 * Since the screen view is drawn from top-left to bottom-right, the 
 * 
 * @author afoltan
 *
 */
public class GridTransform {

	public static final GridTransform blueOnBottom = new GridTransform(TeamColor.BLUE);
	public static final GridTransform redOnBottom = new GridTransform(TeamColor.RED);
	
	private TeamColor bottomScreenColor;
	
	// private default constructor
	private GridTransform() {}
	
	/**
	 * Constructs a TransformLocation object based on the color that 
	 * will be presented ***on the bottom of the screen***
	 * @param color
	 */
	private GridTransform(TeamColor bottomScreenColor) {
		this.bottomScreenColor = bottomScreenColor;
	}

	/**
	 * Returns one of the two singletons for this class
	 * @param bottomScreenColor = the color of the player (bottom of screen)
	 * @return
	 */
	public static GridTransform getIstance( TeamColor bottomScreenColor ) {
		if (bottomScreenColor == TeamColor.BLUE) {
			return blueOnBottom;			
		} else {
			return redOnBottom;
		}
	}
	
	/**
	 * When RED is on the bottom, 
	 * 
	 * BoardSquare(row,col) = Location(X,Y) = Index
	 * BoardSquare(0,0)     = Location(0,9) = 90
	 * BoardSquare(0,1)     = Location(1,9) = 91
	 * ...
	 * BoardSquare(9,9)     = Location(9,0) = 9
	 * 
	 *    row = 9 - Y;  col = X;
	 * 
	 * When BLUE is on the bottom,
	 * 
	 * BoardSquare(row,col) = Location(X,Y) = Index
	 * BoardSquare(0,0)     = Location(9,0) = 9
	 * BoardSquare(0,1)     = Location(8,0) = 8
	 * ...
	 * BoardSquare(9,9)     = Location(0,9) = 90
	 *  
	 *    row = Y ; col = 9 - X;
	 *    
	 * @return
	 */	
	public Location boardSquareToLocation( GameBoardSquare square ) {
		int X, Y;
		if(square == null)
			System.out.println("Here");
		if (bottomScreenColor == TeamColor.RED) {
			X = square.getCol();
			Y = 9-square.getRow();
		} else {
			X = 9 - square.getCol();
			Y = square.getRow();
		}
		return new Location(X,Y);
	}

	public Location boardSquareToLocation( int row, int col ) {
		int X, Y;
		if (bottomScreenColor == TeamColor.RED) {
			X = col;
			Y = 9-row;
		} else {
			X = 9 - col;
			Y = row;
		}
		return new Location(X,Y);
	}

	public int locationToBoardSquare( Location loc ) {
		int row, col;
		if (bottomScreenColor == TeamColor.RED) {
			row = 9 - loc.getYCoordinate();
			col = loc.getXCoordinate();
		} else {
			row = loc.getYCoordinate();
			col = 9 - loc.getXCoordinate();
		}
		return (row*10) + col;
	}
	
	public TeamColor getBottomColor() {
		return bottomScreenColor;
	}
	
}
